package cn.ming.leo.utils;

import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;

import com.avaje.ebean.Expr;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.SqlQuery;

/**
 * @author liaozhida 数据库表达式
 */
public class SqlUtils {

	public final static Integer INT = 0;
	public final static Integer STRING = 1;
	public final static Integer STRINGGROUP = 2;

	/**
	 * 返回处理过的表达式
	 * 
	 * @param key
	 *            字段名
	 * @param value
	 *            字段值
	 * @param keyType
	 *            字段类型 默认是字符串
	 * @param dbStatement
	 *            条件
	 * @param pression
	 *            表达式
	 * @return pression
	 */
	public static ExpressionList getExpression(String key, Object value, Integer keyType, Predicate predicate,
			ExpressionList pression) {

		Object valueObject = null;
		// 入参类型
		if (STRING == keyType) {
			if ("".equals(value) || value == null) {
				return pression;
			} else {
				valueObject = value;
			}
		} else if (INT == keyType) {
			if (!NumberUtils.isNumber((String) value)) {
				return pression;
			} else {
				valueObject = Integer.parseInt((String) value);
			}
		} else if (STRINGGROUP == keyType) {
			if (value == null) {
				return pression;
			} else {
				valueObject = value;
			}
		}
		// 筛选条件
		switch (predicate) {
		case eq:
			pression = pression.eq(key, valueObject);
			break;
		case ge:
			pression = pression.ge(key, valueObject);
			break;
		case gt:
			pression = pression.gt(key, valueObject);
			break;
		case le:
			pression = pression.le(key, valueObject);
			break;
		case ne:
			pression = pression.ne(key, valueObject);
			break;
		case notNull:
			pression = pression.isNotNull(key);
			break;
		case isNull:
			pression = pression.isNull(key);
			break;
		case likeHead:
			pression = pression.like(key, valueObject + "%");
			break;
		case likeAll:
			pression = pression.like(key, "%" + valueObject + "%");
			break;
		case likeTail:
			pression = pression.like(key, "%" + valueObject);
			break;
		case in:
			List<String> list = (List<String>) value;
			if (list.size() != 0 && list != null) {
				pression = pression.in(key, list);
			}
			break;
		case between:
			List<String> listbetween = (List<String>) value;
			if (listbetween.size() != 0 && listbetween != null) {
				pression.between(key, listbetween.get(0), listbetween.get(1));
			}
			break;
		case or:
			String[] orList = (String[]) value;
			if (orList.length != 0 && orList != null) {
				pression.or(Expr.eq(key, orList[0]), Expr.eq(key, orList[1]));
			}
			break;
		default:
			break;
		}
		return pression;
	}

	/**
	 * 减少代码中if语句的出现
	 * 
	 * @param sql
	 * @param parameters
	 * @param sqlParts
	 * @return
	 */
	public static String makeSql(String sql, String parameters, String sqlParts) {
		if (parameters != null && parameters != "") {
			sql = sql + " " + sqlParts + " ";
		}
		return sql;
	}

	public static SqlQuery makeSqlQuery(SqlQuery sqlQuery, String parameterName, String value) {
		if (value != null && value != "") {
			sqlQuery = sqlQuery.setParameter(parameterName, value);
		}
		return sqlQuery;
	}
}
